package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.config.AliPay;
import cn.itsource.ymcc.constant.OrderStatusConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.dto.PayOrderDto;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@RocketMQMessageListener(
        consumerGroup = "OrderDelayConsumer",
        topic = OrderStatusConstants.TOPIC_COURSE_ORDER_DELAY,
        selectorExpression = OrderStatusConstants.TAGS_COURSE_ORDER_DELAY
)
@Slf4j
public class OrderDelayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private PayOrderMapper payOrderMapper;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            //补偿
            byte[] body = messageExt.getBody();
            String s = new String(body, "utf-8");
            CourseOrder courseOrder = JSONObject.parseObject(s, CourseOrder.class);
            String orderNo = courseOrder.getOrderNo();

            PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));
            if(OrderStatusConstants.ORDER_DEFAULT.equals(payOrder.getPayStatus())){
                //去查询  alipay 看用户是否已经支付
                //如果支付了  调用  写你的逻辑
                System.out.println("系统未处理 需要使用补偿机制");
            }else {
                System.out.println("系统自动处理了");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
